﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using vnFingerPrint.BusinessObject;
using vnCommon;
namespace vnFingerPrint.DataAccess
{
    public static class dmQuanHuyen_DataAccess
    {
        //Select dmQuanHuyen y tinh tanh
        public static List<dmQuanHuyen_Object> SelectByIdTinhThanh(int _Id_TinhThanh)
        {
            string SelectQuery = "Select * from [dmQuanHuyen] where (1=1) and [Id_TinhThanh]= @Id_TinhThanh";
            List<SqlParameter> paralist = new List<SqlParameter>();
            paralist.Add(new SqlParameter("Id_TinhThanh", _Id_TinhThanh));
            List<dmQuanHuyen_Object> result = new List<dmQuanHuyen_Object>();
            DataTable tb = commonDatabase.GetSQLDataTable(clsConnectionString.GetConnectionString(), SelectQuery,paralist);
            foreach (DataRow row in tb.Rows)
            {
                dmQuanHuyen_Object newObject = new dmQuanHuyen_Object();
                newObject.Id = (int)row["Id"];
                if (row["TenQuanHuyen"].ToString() != "") newObject.TenQuanHuyen = (string)row["TenQuanHuyen"];
                if (row["Id_TinhThanh"].ToString() != "") newObject.Id_TinhThanh = (int)row["Id_TinhThanh"];
                result.Add(newObject);
            }
            return result;
        }
        //Select dmQuanHuyen
        public static List<dmQuanHuyen_Object> SelectAll()
        {
            string SelectQuery = @"Select * from [dmQuanHuyen]";
            List<dmQuanHuyen_Object> result = new List<dmQuanHuyen_Object>();
            DataTable tb = commonDatabase.GetSQLDataTable(clsConnectionString.GetConnectionString(), SelectQuery);
            foreach (DataRow row in tb.Rows)
            {
                dmQuanHuyen_Object newObject = new dmQuanHuyen_Object();
                newObject.Id = (int)row["Id"];
                if (row["TenQuanHuyen"].ToString() != "") newObject.TenQuanHuyen = (string)row["TenQuanHuyen"];
                if (row["Id_TinhThanh"].ToString() != "") newObject.Id_TinhThanh = (int)row["Id_TinhThanh"];
                result.Add(newObject);
            }
            return result;
        }
        //Insert dmQuanHuyen
        public static int Insert(dmQuanHuyen_Object newObject)
        {
            string InsertQuery = "Insert Into [dmQuanHuyen]";
            List<SqlParameter> paralist = new List<SqlParameter>();
            string _val = "", _para = "";
            _val += "Id, "; _para += "@Id, ";
            paralist.Add(new SqlParameter("Id", newObject.Id));
            if (newObject.TenQuanHuyen != null)
            {
                _val += "TenQuanHuyen, "; _para += "@TenQuanHuyen, ";
                paralist.Add(new SqlParameter("TenQuanHuyen", newObject.TenQuanHuyen));
            }
            if (newObject.Id_TinhThanh != null)
            {
                _val += "Id_TinhThanh, "; _para += "@Id_TinhThanh, ";
                paralist.Add(new SqlParameter("Id_TinhThanh", newObject.Id_TinhThanh));
            }
            InsertQuery += "(" + _val + ") Values (" + _para + ")";
            InsertQuery = InsertQuery.Replace(", )", ")");
            return commonDatabase.ExecuteNonQuerySQL(clsConnectionString.GetConnectionString(), InsertQuery, paralist);
        }
        //Get an Object of dmQuanHuyen
        public static dmQuanHuyen_Object GetObject(int _Id)
        {
            List<SqlParameter> paralist = new List<SqlParameter>();
            dmQuanHuyen_Object newObject = new dmQuanHuyen_Object();
            string SelectQuery = "Select * from [dmQuanHuyen] where (1=1) and [Id]= @Id";
            paralist.Add(new SqlParameter("Id", _Id));
            DataTable tb = commonDatabase.GetSQLDataTable(clsConnectionString.GetConnectionString(), SelectQuery, paralist);
            if (tb.Rows.Count < 1) return null;
            DataRow row = tb.Rows[0];
            newObject.Id = (int)row["Id"];
            if (row["TenQuanHuyen"].ToString() != "") newObject.TenQuanHuyen = (string)row["TenQuanHuyen"];
            if (row["Id_TinhThanh"].ToString() != "") newObject.Id_TinhThanh = (int)row["Id_TinhThanh"];
            return newObject;
        }
        //Save changes of an Object to database dmQuanHuyen
        public static int SaveChanged(dmQuanHuyen_Object UpdatingObject)
        {
            List<SqlParameter> paralist = new List<SqlParameter>();
            string UpdateQuery =
            @"Update dmQuanHuyen SET
               TenQuanHuyen= @TenQuanHuyen,
               Id_TinhThanh= @Id_TinhThanh
            Where Id= @Id";
            paralist.Add(new SqlParameter("Id", UpdatingObject.Id));
            paralist.Add(new SqlParameter("TenQuanHuyen", UpdatingObject.TenQuanHuyen));
            paralist.Add(new SqlParameter("Id_TinhThanh", UpdatingObject.Id_TinhThanh));
            return commonDatabase.ExecuteNonQuerySQL(clsConnectionString.GetConnectionString(), UpdateQuery, paralist);
        }
        //Delete an Object in database dmQuanHuyen
        public static int Delete(dmQuanHuyen_Object DeletingObject)
        {
            List<SqlParameter> paralist = new List<SqlParameter>();
            string DeleteQuery =
            @"Delete [dmQuanHuyen]
            Where Id= @Id";
            paralist.Add(new SqlParameter("Id", DeletingObject.Id));
            return commonDatabase.ExecuteNonQuerySQL(clsConnectionString.GetConnectionString(), DeleteQuery, paralist);
        }
    }
}